package com.gmrz.uaf.protocol.v1.processor;

import com.gmrz.uaf.db.DAOException;
import com.gmrz.uaf.db.UAFDAOFactory;
import com.gmrz.uaf.db.UAFDBConnectionMgr;
import com.gmrz.uaf.db.dao.AuthenticatorDAO;
import com.gmrz.uaf.db.dao.DevicesDao;
import com.gmrz.uaf.protocol.v1.json.UAFSchemaBuilder;
import com.gmrz.uaf.protocol.v1.processor.exception.AdditionUviException;
import com.gmrz.uaf.protocol.v1.processor.exception.UAFErrorCode;
import com.gmrz.uaf.protocol.v1.schema.*;
import com.gmrz.util.Convert;
import com.gmrz.util.db.DBUtil;
import com.google.common.reflect.TypeToken;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by chentaikun on 16/12/19.
 */
public class UAFGetCertInfoProcessor extends UAFBaseProcessor {

    private static final Logger LOG = LogManager.getLogger(UAFGetCertInfoProcessor.class);

    /**
     *
     * @param userName
     * @param deviceId
     * @throws SQLException
     * @throws DAOException
     */
    public List<CertificateShowBean> getCertInfos(String userName,String deviceId,String appID) throws SQLException, DAOException {
        Connection conn = UAFDBConnectionMgr.getConnection();
        try {
            AuthenticatorDAO adao = UAFDAOFactory.createAuthenticatorDAO(conn);
            DevicesDao devicesDAO = UAFDAOFactory.createDevicesDAO(conn);
            List<String> authType = new ArrayList<String>();
            authType.add("20");
            authType.add("21");
            authType.add("22");
            List<Authenticator> authenticatorList = adao.findAuthenticatorAndCertInfo(userName, deviceId,authType,appID);
            List<CertificateShowBean> certificateShowBeanList = new ArrayList<CertificateShowBean>();
            if(null != authenticatorList){
                for(Authenticator authenticator : authenticatorList){
                    CertificateExtendBean certificateExtend = authenticator.getCertificateExtendBean();
                    DeviceDetails deviceDetails = devicesDAO.findOne(authenticator.getAppID(),authenticator.getUserName(),authenticator.getDeviceID(),authenticator.getAuthType(),authenticator.getTransType());
                    CertificateShowBean certificateShowBean = new CertificateShowBean();
                    certificateShowBean.setUserName(authenticator.getCustNo());
                    certificateShowBean.setCertDN(Convert.toBase64(certificateExtend.getCertDN().getBytes()));
                    certificateShowBean.setSerialNumber(certificateExtend.getSerialNumber());
                    certificateShowBean.setIssuedTime(certificateExtend.getIssuedTime().getTime());
                    certificateShowBean.setExpireTime(certificateExtend.getExpireTime().getTime());
                    certificateShowBean.setCertStatus(certificateExtend.getCertStatus());
                    certificateShowBean.setActivateTime(null != certificateExtend.getActivateTime() ? certificateExtend.getActivateTime().getTime():0);
                    certificateShowBean.setCancelTime(null != certificateExtend.getCancelTime() ? certificateExtend.getCancelTime().getTime():0);
                    certificateShowBean.setAppID(authenticator.getAppID());
                    certificateShowBean.setActivateAppID(certificateExtend.getActivateAppid());
                    certificateShowBean.setActivateType(certificateExtend.getActivateType());
                    certificateShowBean.setCancelAppID(certificateExtend.getCancelAppid());
                    certificateShowBean.setCancelReason(certificateExtend.getCancelReason());
                    certificateShowBean.setCertType(certificateExtend.getCertType());
                    certificateShowBean.setDeviceID(authenticator.getDeviceID());
                    certificateShowBean.setDeviceName(deviceDetails.getDeviceName());
                    certificateShowBean.setDeviceType(deviceDetails.getDeviceType());
                    String securityLevel = this.serverConfig.getDictConfig(authenticator.getAAID().toString().toUpperCase());
                    certificateShowBean.setSecurityLevel(securityLevel);
                    certificateShowBeanList.add(certificateShowBean);
                }
            }

            return certificateShowBeanList;
        }catch (DAOException e1){
            LOG.error("getCertInfos error",e1);
            throw e1;
        }finally {
            DBUtil.close(conn);
        }
    }
}
